package love.yuanmengrobot.guoyitanghoutai.service.impl;
/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:CHJY创幻
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/8/17 19:52
 */

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import love.yuanmengrobot.guoyitanghoutai.dto.TbNatmehaHotspotAndPage;
import love.yuanmengrobot.guoyitanghoutai.mapper.TbNatmehaHotspotMapper;
import love.yuanmengrobot.guoyitanghoutai.pojo.TbNatmehaHotspot;
import love.yuanmengrobot.guoyitanghoutai.service.TbNatmehaHotspotService;
import love.yuanmengrobot.guoyitanghoutai.util.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class TbNatmehaHotspotServiceImpl implements TbNatmehaHotspotService {
    /**
     * 所需表对应列名
     */
    private static final String COLUMN_HOTSPOT_TITLE = "hotspot_title";
    private static final String COLUMN_HOTSPOT_SOURCE = "hotspot_source";
    private static final String COLUMN_HOTSPOT_AUTHOR = "hotspot_author";
    private static final String COLUMN_ITEM_CREATE_AT = "itemCreateAt";
    private static final String COLUMN_DATA_STATUS = "data_status";
    private static final String COLUMN_ITEM_CODE = "itemCode";
    private static final String COLUMN_HOTSPOT_CONTENT = "hotspot_content";


    @Resource
    TbNatmehaHotspotMapper tbNatmehaHotspotMapper;
    @Autowired
    RedisTemplate redisTemplate;
//    private Object IPage;

    @Override
    public IPage<TbNatmehaHotspot> selectHealthPreservation(int current, int limit, String dataType) {

        IPage<TbNatmehaHotspot> pageCourse = new Page<>(current, limit);
        QueryWrapper<TbNatmehaHotspot> queryWrapper = new QueryWrapper<>();
        //根据创建时间排序
        queryWrapper.orderByDesc("itemcreateat");
        queryWrapper.eq("data_type", dataType);
        pageCourse = this.tbNatmehaHotspotMapper.selectPage(pageCourse, queryWrapper);


//        List<TbNatmehaHotspot> list =this.tbNatmehaHotspotMapper.selectPage(pageCourse, queryWrapper).getRecords();

//        System.out.println(pageCourse.getTotal() + "#" + pageCourse.getSize());
        String key="getHealthPreservation:"+current+":"+limit;

      String a=  JsonXMLUtils.obj2json(pageCourse);
//        IPage<TbNatmehaHotspot> pageCourse2=JsonXMLUtils.json2obj(a,pageCourse.getClass());
        redisTemplate.opsForValue().set(key,a);


        return pageCourse;

    }

    @Override
    public IPage<TbNatmehaHotspot> selectHealthPreservationByHotspotTitle(int current, int limit, String dataType, String hotspotTitle) {
        IPage<TbNatmehaHotspot> pageCourse = new Page<>(current, limit);
        QueryWrapper<TbNatmehaHotspot> queryWrapper = new QueryWrapper<>();
        //根据创建时间排序
        queryWrapper.orderByDesc("itemcreateat");
        queryWrapper.eq("data_type", dataType);
        if(hotspotTitle!=null)
        queryWrapper.like("hotspot_title", hotspotTitle);
        pageCourse = this.tbNatmehaHotspotMapper.selectPage(pageCourse, queryWrapper);

        return pageCourse;
    }

    @Override
    public int insertHealthPreservation(TbNatmehaHotspot tbNatmehaHotspot) {

        return    tbNatmehaHotspotMapper.insert(tbNatmehaHotspot);
//        return 0;
    }

    @Override
    public int updateHealthPreservation(TbNatmehaHotspot tbNatmehaHotspot) {
        System.out.println("#@#"+tbNatmehaHotspot);
        UpdateWrapper<TbNatmehaHotspot> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("itemcode",tbNatmehaHotspot.getItemcode());

        return tbNatmehaHotspotMapper.update(tbNatmehaHotspot,updateWrapper);
    }

    @Override
    public int updateHealthPreservationStatus(String itemcode, String status) {

        UpdateWrapper<TbNatmehaHotspot> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("itemcode",itemcode);
        TbNatmehaHotspot tbNatmehaHotspot = new TbNatmehaHotspot();
        tbNatmehaHotspot.setDataStatus(status);
        return  tbNatmehaHotspotMapper.update(tbNatmehaHotspot, updateWrapper);

    }

    @Override
    public int deleteHealthPreservation(String itemcode) {
//        tbNatmehaHotspotMapper.delete()


        // 按照条件删除，参数是一个map集合，一般适合简单的条件删除
        Map<String, Object> map = new HashMap<>();

        map.put("itemcode", itemcode);

        int count = tbNatmehaHotspotMapper.deleteByMap(map);

        return count;
    }

    /**
     * 实现儿童健康查询
     *
     * @param page 当前页码
     * @return 当前页数据
     */
    @Override
    public IPage<TbNatmehaHotspot> selectChildrenHealth(IPage<TbNatmehaHotspot> page) {
        QueryWrapper<TbNatmehaHotspot> wrapper = new QueryWrapper<>();
        wrapper.select(COLUMN_HOTSPOT_TITLE, COLUMN_HOTSPOT_SOURCE, COLUMN_HOTSPOT_AUTHOR, COLUMN_ITEM_CREATE_AT,
                COLUMN_DATA_STATUS, COLUMN_ITEM_CODE);
        return tbNatmehaHotspotMapper.selectPage(page, wrapper);
    }

    @Override
    public Vo selectMedicineDiet(Parameters params) {
        IPage<TbNatmehaHotspot> hotspotIPage=new Page<>(params.getCurrent(),params.getSize());
        hotspotIPage=tbNatmehaHotspotMapper.selectPage(hotspotIPage,new QueryWrapper<TbNatmehaHotspot>()
                .eq(params.getDataType()!=null,"data_type",params.getDataType()));
        Vo<TbNatmehaHotspot> voMedicineDietList=new Vo<>();
        voMedicineDietList.setCurrent(params.getCurrent());
        voMedicineDietList.setSize(params.getSize());
        voMedicineDietList.setDataType(params.getDataType());
        voMedicineDietList.setList(hotspotIPage.getRecords());
        voMedicineDietList.setTotal(hotspotIPage.getTotal());
        return voMedicineDietList;
    }

    /**
     * 实现新增儿童健康记录
     *
     * @param entity 记录内容
     * @return 成功 OR 失败
     */
    @Override
    public boolean insertChildrenHealth(TbNatmehaHotspot entity) {
        entity.setDataType("4");
        return tbNatmehaHotspotMapper.insert(entity) == 1;
    }

    /**
     * 实现删除儿童健康记录
     *
     * @param itemCode 唯一标识符
     * @return 成功 OR 失败
     */
    @Override
    public boolean deleteChildrenHealth(String itemCode) {
        return tbNatmehaHotspotMapper.delete(
                new QueryWrapper<TbNatmehaHotspot>().eq(COLUMN_ITEM_CODE, itemCode)) == 1;
    }

    /**
     * 实现修改儿童健康记录
     *
     * @param entity 修改内容
     * @return 成功 OR 失败
     */
    @Override
    public boolean updateChildrenHealth(TbNatmehaHotspot entity) {
        return tbNatmehaHotspotMapper.update(entity,
                new UpdateWrapper<TbNatmehaHotspot>().eq(COLUMN_ITEM_CODE, entity.getItemcode())) == 1;
    }

    /**
     * 实现查询查看儿童健康记录内容
     *
     * @param itemCode 唯一标识符
     * @return 内容
     */
    @Override
    public String selectChildrenHeathContent(String itemCode) {
        return tbNatmehaHotspotMapper.selectOne(
                new QueryWrapper<TbNatmehaHotspot>().select(COLUMN_HOTSPOT_CONTENT).eq(COLUMN_ITEM_CODE, itemCode))
                .getHotspotContent();
    }

    @Override
    public int selectPageSum(int size) {
        int sum = tbNatmehaHotspotMapper.selectCount(null);
        return sum % size == 0 ? sum / size : sum / size + 1;
    }





    @Override
    public IPage getAll(TbNatmehaHotspotAndPage tbNatmehaHotspotAndPage) {
        LambdaQueryWrapper<TbNatmehaHotspot> wrapper = new QueryWrapper<TbNatmehaHotspot>().lambda();
        String status=tbNatmehaHotspotAndPage.getTbNatmehaHotspot().getDataStatus();
        if(!"9".equals(status)){
            wrapper= wrapper.eq(TbNatmehaHotspot::getDataStatus, status);
        }

        if(tbNatmehaHotspotAndPage.getTbNatmehaHotspot().getUserCode()!=null){
            wrapper= wrapper.eq(TbNatmehaHotspot::getUserCode,tbNatmehaHotspotAndPage.getTbNatmehaHotspot().getUserCode());
        }
        wrapper=wrapper.eq(TbNatmehaHotspot::getDataType,tbNatmehaHotspotAndPage.getTbNatmehaHotspot().getDataType());

        if(tbNatmehaHotspotAndPage.getStartTime()!=null){
            wrapper=wrapper.gt(TbNatmehaHotspot::getItemcreateat,tbNatmehaHotspotAndPage.getStartTime());
        }
        if(tbNatmehaHotspotAndPage.getEndTime()!=null){
            wrapper=wrapper.lt(TbNatmehaHotspot::getItemcreateat,tbNatmehaHotspotAndPage.getEndTime());
        }
        System.out.println(tbNatmehaHotspotAndPage.getTbNatmehaHotspot().getHotspotTitle());
        if(StringUtils.isNoneBlank(tbNatmehaHotspotAndPage.getTbNatmehaHotspot().getHotspotTitle())){
            wrapper.and(wrapperr->wrapperr.like(TbNatmehaHotspot::getHotspotTitle,tbNatmehaHotspotAndPage.getTbNatmehaHotspot().getHotspotTitle())
                    .or()
                    .like(TbNatmehaHotspot::getHotspotAuthor,tbNatmehaHotspotAndPage.getTbNatmehaHotspot().getHotspotTitle())
                    .or()
                    .like(TbNatmehaHotspot::getHotspotSource,tbNatmehaHotspotAndPage.getTbNatmehaHotspot().getHotspotTitle()));
        }

        return tbNatmehaHotspotMapper.selectPage(tbNatmehaHotspotAndPage.getPage(), wrapper);
    }

    @Override
    public boolean insertTraditionalCultural(TbNatmehaHotspot tbNatmehaHotspot) {
        tbNatmehaHotspot.setItemid(null);
        tbNatmehaHotspot.setItemcode(UUIDUtils.getUUID());
        SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = DateUtils.getDate();
        tbNatmehaHotspot.setItemcreateat(parse);
        tbNatmehaHotspot.setItemupdateat(parse);

        System.out.println(tbNatmehaHotspot);
        return tbNatmehaHotspotMapper.insert(tbNatmehaHotspot)==1;
    }

    @Override
    public boolean editTraditionalCultural(TbNatmehaHotspot tbNatmehaHotspot) {
        LambdaQueryWrapper<TbNatmehaHotspot> wrapper = new QueryWrapper<TbNatmehaHotspot>().lambda();
        wrapper.eq(TbNatmehaHotspot::getItemcode,tbNatmehaHotspot.getItemcode());
        return tbNatmehaHotspotMapper.update(tbNatmehaHotspot,wrapper)==1;
    }

    @Override
    public boolean deleTraditionalCultural(TbNatmehaHotspot tbNatmehaHotspot) {
        LambdaQueryWrapper<TbNatmehaHotspot> wrapper = new QueryWrapper<TbNatmehaHotspot>().lambda();
        wrapper.eq(TbNatmehaHotspot::getItemid,tbNatmehaHotspot.getItemid());
        return tbNatmehaHotspotMapper.delete(wrapper)==1;
    }
}
